package smedi.ismedi.mobile.bff.controller.file;

import com.smedi.ismedi.file.dto.response.FileInfoResponse;
import com.smedi.ismedi.file.feign.RemoteFileFeignApi;
import com.smedi.ismedi.web.starter.common.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import smedi.ismedi.mobile.bff.controller.BaseController;
import smedi.ismedi.mobile.bff.infrastructure.util.FileHelper;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

/**
 * @Author Jimmy
 * @Date 2021/6/3 17:11
 */
@Api(value = "文件管理", tags = {"文件管理"})
@RequestMapping(value = "/mobilebff/v1/file")
@RestController
@RequiredArgsConstructor
public class FileController extends BaseController {
    private final RemoteFileFeignApi fileFeignApi;

    @Value("${fdfs.service.address.in}")
    private String FDFS_SERVICE_ADDRESS_IN;

    @Value("${fdfs.service.address.out}")
    private String FDFS_SERVICE_ADDRESS_OUT;

    /**
     * 文件上传
     *
     * @param file
     * @return
     */
    @ApiOperation(value = "文件上传", nickname = "文件上传", httpMethod = "POST", response = ResponseData.class)
    @PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    ResponseData<FileInfoResponse> fileUpLoad(@ApiParam(value = "file", required = true) @RequestPart(value = "file", required = true) MultipartFile file) {
        return fileFeignApi.fileUpLoad(file);
    }

    /**
     * 上传文件确认
     *
     * @param signKey
     * @return
     */
    @ApiOperation(value = "上传文件确认", nickname = "上传文件确认", response = ResponseData.class)
    @GetMapping(value = "/uploadFileConfirm")
    ResponseData<Boolean> uploadFileConfirm(@ApiParam(value = "signKey", required = true) @RequestParam(value = "signKey", required = true) String signKey) {
        return fileFeignApi.uploadFileConfirm(signKey);
    }

    /**
     * 获取文件信息
     *
     * @param signKey
     * @return
     */
    @ApiOperation(value = "获取文件信息", nickname = "获取文件信息", response = ResponseData.class)
    @GetMapping(value = "/getFileInfoBySignKey")
    ResponseData<FileInfoResponse> getFileInfo(@ApiParam(value = "signKey", required = true) @RequestParam(value = "signKey", required = true) String signKey) {
        return fileFeignApi.getFileInfo(signKey);
    }


    /**
     * 删除文件
     *
     * @param signKey
     * @param reason
     * @return
     */
    @ApiOperation(value = "删除文件", nickname = "删除文件", response = ResponseData.class)
    @DeleteMapping(value = "/delete")
    ResponseData<Boolean> deleteFileBySignKey(@NotNull @ApiParam(value = "signKey", required = true)
                                              @RequestParam(value = "signKey", required = true) String signKey, @NotNull @ApiParam(value = "删除原因", required = true)
                                              @RequestParam(value = "reason", required = true) String reason) {
        return fileFeignApi.deleteFileBySignKey(signKey, reason);
    }

    /**
     * 根据下载密钥下载文件
     *
     * @param signKey
     * @return
     */
    @ApiOperation(value = "根据下载密钥下载文件", nickname = "根据下载密钥下载文件", response = ResponseData.class)
    @GetMapping(value = "/downloadFileBySignKey")
    ResponseData<Boolean> downloadFileBySignKey(@NotNull @ApiParam(value = "signKey", required = true)
                                                @RequestParam(value = "signKey", required = true) String signKey, HttpServletResponse response) {
        FileInfoResponse fileInfoResponse = fileFeignApi.getFileInfo(signKey).getData();
        if (fileInfoResponse == null) {
            return new ResponseData<Boolean>().failure("file", "9999", "下载失败");
        }
        FileHelper.downloadFile(FDFS_SERVICE_ADDRESS_IN, fileInfoResponse.getStorageGroup(), fileInfoResponse.getPath(), fileInfoResponse.getName(), response);
        return new ResponseData<>().success(true);
    }
}
